package top.xiaoshuaila.moments.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import top.xiaoshuaila.moments.pojo.Fans;
import top.xiaoshuaila.moments.utils.Dateutils;

import java.sql.Timestamp;
import java.util.List;

@Repository
public interface FocuMapper {
    @Select("select \n" +
            "  b.id,\n" +
            "  b.realname,\n" +
            "  b.headphoto,\n" +
            "  b.introduction,\n" +
            "  (select \n" +
            "    COUNT(*) \n" +
            "  from\n" +
            "    focus \n" +
            "  where focus.userid = ${id} \n" +
            "    and focus.followerid = a.followerid) > 0 isfan,\n" +
            "  (select \n" +
            "    COUNT(*) \n" +
            "  from\n" +
            "    focus \n" +
            "  where focus.userid = a.followerid \n" +
            "    and focus.followerid = ${id}) > 0 isfocuto,\n" +
            "  a.followerid = ${id} isself,\n" +
            "  (select \n" +
            "    COUNT(*) \n" +
            "  from\n" +
            "    blacklist \n" +
            "  where blacklist.userid = ${id} \n" +
            "    and blacklist.blacklistid = a.followerid) > 0 " +
            "isblack \n" +
            "from\n" +
            "  focus a \n" +
            "  left join users b \n" +
            "    on a.followerid = b.id \n" +
            "where a.userid = ${vid}  order by a.time desc limit ${page},${num}")
    List<Fans> getfans(long id, long vid,int page,int num);
    @Select("select COUNT(*)>0 from blacklist b where b.userid=${id} and b.blacklistid=${vid} ")
    boolean ispinbi(long id, long vid);
    @Insert("insert into blacklist (userid,blacklistid)values (${id},${vid})")
    void setblack(long id, long vid);
    @Delete("delete from blacklist where userid=${id} and blacklistid=${vid}")
    void removeblack(long id, long vid);
    @Select("select \n" +
            "  (select \n" +
            "    COUNT(*) \n" +
            "  from\n" +
            "    focus \n" +
            "  where userid = ${id} \n" +
            "    and followerid = ${vid}) *2+\n" +
            "  (select \n" +
            "    COUNT(*) \n" +
            "  from\n" +
            "    focus \n" +
            "  where userid = ${vid} \n" +
            "    and followerid = ${id}) ")
    int guanzhu(long id, long vid);

    @Delete("delete from focus where followerid=${id} and userid=${vid}")
    void removefocuto(long id, long vid);
    @Insert("insert into focus (userid,followerid,time)values (${vid},${id},'${time}')")
    void addfocuto(long id, long vid, Timestamp time);

    @Select("select \n" +
            "  b.id,\n" +
            "  b.realname,\n" +
            "  b.headphoto,\n" +
            "  b.introduction,\n" +
            "  (select \n" +
            "    COUNT(*) \n" +
            "  from\n" +
            "    focus \n" +
            "  where focus.userid = ${id} \n" +
            "    and focus.followerid = a.userid) > 0 isfan,\n" +
            "  (select \n" +
            "    COUNT(*) \n" +
            "  from\n" +
            "    focus \n" +
            "  where focus.userid = a.userid \n" +
            "    and focus.followerid = ${id}) > 0 isfocuto,\n" +
            "  a.followerid = ${id} isself\n" +
            "from\n" +
            "  focus a \n" +
            "  left join users b \n" +
            "    on a.userid = b.id \n" +
            "where a.followerid = ${vid}  order by a.time desc limit ${page},${num}")
    List<Fans> getfocuto(long id, long vid,int page,int num);
    @Select("select count(*)\n" +
            "from\n" +
            "  focus a \n" +
            "  left join users b \n" +
            "    on a.userid = b.id \n" +
            "where a.followerid = ${vid} ")
    int gettotalfocuto(long id,long vid);
    @Select("select count(*)\n" +
            "from\n" +
            "  focus a \n" +
            "  left join users b \n" +
            "    on a.followerid = b.id \n" +
            "where a.userid = ${vid} ")
    int gettotalfans(long id, long vid);
}
